package cn.hxzy.dao;

import cn.hxzy.entity.*;
import cn.hxzy.util.DataSource;
import sun.security.util.Password;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao extends BaseDao<User> {

    public User findByLoginName(String loginName) throws Exception {
        Connection connection = DataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement("select * from user where name =?");
        statement.setObject(1, loginName);
        ResultSet resultSet = statement.executeQuery();
        User user = null;
        if (resultSet.next()) {
            user = new User(resultSet.getString("name"), resultSet.getString("password"));
            user.setId(resultSet.getInt("id"));
        }
        resultSet.close();
        statement.close();
        DataSource.closeConnection(connection);
        return user;
    }

    /**
     * 查询id相关数据
     */
    public List<User> findByIdLike(Integer page, Integer size, int id) throws SQLException {
        Connection connection = DataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement("SELECT * FROM USER WHERE id=? limit ?,?");
        statement.setObject(1, id);
        statement.setObject(2, (page - 1) * size);
        statement.setObject(3, size);
        ResultSet resultSet = statement.executeQuery();
        List<User> list = new ArrayList<>();
        while (resultSet.next()) {
            User user = new User(resultSet.getString("name"), resultSet.getString("password"));
            user.setId(resultSet.getInt("id"));
            list.add(user);
        }
        resultSet.close();
        statement.close();
        DataSource.closeConnection(connection);
        return list;
    }

    /**
     * 查询名字相关数据
     */
    public List<User> findByNameLike(Integer page, Integer size, String name) throws SQLException {
        Connection connection = DataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement("SELECT * FROM USER WHERE name like ? limit ?,?");
        statement.setObject(1, "%"+name+"%");
        statement.setObject(2, (page - 1) * size);
        statement.setObject(3, size);
        ResultSet resultSet = statement.executeQuery();
        List<User> list = new ArrayList<>();
        while (resultSet.next()) {
            User user = new User(resultSet.getString("name"), resultSet.getString("password"));
            user.setId(resultSet.getInt("id"));
            list.add(user);
        }
        System.out.println("************"+list+"**************");
        resultSet.close();
        statement.close();
        DataSource.closeConnection(connection);
        return list;
    }

    /**
     *查询个数——>关于名字的内容
     */
    public long countAllWithName(String name) throws SQLException {
        Connection connection = DataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM user WHERE name like ?");
        statement.setObject(1, "%"+name+"%");
        ResultSet resultSet = statement.executeQuery();
        long count = 0;
        if (resultSet.next()) {
            count = resultSet.getLong(1);
        }
        resultSet.close();
        statement.close();
        DataSource.closeConnection(connection);
        System.out.println("*****************\n" + count + "\n*****************");
        return count;
    }

    /**
     *查询个数——>关于id的内容
     */
    public long countAllWithId(int id) throws SQLException {
        Connection connection = DataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM user WHERE n.id= ?");
        statement.setObject(1, id);
        ResultSet resultSet = statement.executeQuery();
        long count = 0;
        if (resultSet.next()) {
            count = resultSet.getLong(1);
        }
        resultSet.close();
        statement.close();
        DataSource.closeConnection(connection);
        System.out.println("*****************\n" + count + "\n*****************");
        return count;
    }
}

